package insert

import (
	"fmt"
	"gorm.io/gorm"
	"shop/gorm_test/global"
	"shop/gorm_test/model"
)

type Order struct {
	model.BaseModel
	OrderNo      string `gorm:"order_no;type:varchar(64);not null;index"`
	UserId       int32  `gorm:"user_id;type:int(10);default:10"`
	Phone        string `gorm:"phone;default:18768233"`
	ActivityId   int32
	ActivityName string
	Order1       Order1 `gorm:"foreignkey:OrderNo;reference:OrderNo"`
}

type Order1 struct {
	model.BaseModel
	OrderNo      string `gorm:"order_no;type:varchar(64);not null;index"`
	UserId       int32  `gorm:"user_id;type:int(10)"`
	Phone        string
	ActivityId   int32
	ActivityName string
}

func xx() {

}

func (o *Order) InsertOne() Order {
	order := Order{
		OrderNo:      o.OrderNo,
		UserId:       o.UserId,
		Phone:        o.Phone,
		ActivityId:   o.ActivityId,
		ActivityName: o.ActivityName,
	}

	global.Db.Create(&order)
	return order
}

func (o *Order) BeforeCreate(tx *gorm.DB) (err error) {
	fmt.Println("BeforeCreate")
	return nil
}

func (o *Order) AfterCreate(tx *gorm.DB) (err error) {

	fmt.Println("AfterCreate")
	return nil
}
func InsertMore(order []Order) *gorm.DB {

	return global.Db.Omit("UserId").Create(&order)
}
